package com.bootdo.wx.service.impl;

import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.common.utils.R;
import com.bootdo.wx.dao.*;
import com.bootdo.wx.domain.MemberPriceDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.domain.OrderComDO;
import com.bootdo.wx.domain.OrderOrderDO;
import com.bootdo.wx.service.OrderComService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Service
public class OrderComServiceImpl implements OrderComService {
	@Autowired
	private OrderComDao orderComDao;
	@Autowired
	private MemberCouponDao memberCouponDao;
	@Autowired
	private MemberExtractDao memberExtractDao;
	@Autowired
	private MemberPriceDao memberPriceDao;
	@Autowired
	private OrderOrderDao orderOrderDao;
	@Autowired
	private GoodsGoodsDao goodsGoodsDao;
	@Autowired
	private MemberUserDao memberUserDao;


	@Override
	public OrderComDO get(String id){
		return orderComDao.get(id);
	}
	
	@Override
	public List<OrderComDO> list(Map<String, Object> map){
		return orderComDao.list(map);
	}

	@Override
	public List<Map<String,Object>> comPriceList(Map<String, Object> map){
		return orderComDao.comPriceList(map);
	}

	@Override
	public List<Map<String,Object>> comUser(Map<String, Object> map){
		return orderComDao.comUser(map);
	}

    @Override
    public BigDecimal sumComUserMoney(Map<String, Object> params) {
		BigDecimal sumComUserMoney = orderComDao.sumComUserMoney(params);
		return sumComUserMoney;
    }

    @Override
	public int count(Map<String, Object> map){
		return orderComDao.count(map);
	}
	
	@Override
	public int save(OrderComDO orderCom){
		return orderComDao.save(orderCom);
	}
	
	@Override
	public int update(OrderComDO orderCom){
		return orderComDao.update(orderCom);
	}
	
	@Override
	public int remove(String id){
		return orderComDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return orderComDao.batchRemove(ids);
	}

	@Override
	public Map<String,Object> getOrderCom(Map<String, Object> map) {
		    Map<String,Object> priceMap = new HashMap<>();
		    map.put("status","2");
			BigDecimal sPrice = orderComDao.getComPrice(map);//已返利
		    map.put("status","0");
		    BigDecimal dPrice = orderComDao.getComPrice(map);//已添加订单
			priceMap.put("sPrice",sPrice.add(dPrice));
			priceMap.put("dPrice",dPrice);
		    return priceMap;
	}

	@Override
	public Map<String,Object> getMemberCom(Map<String, Object> map) {
		map.put("status", 2);
		BigDecimal sPrice = orderComDao.getComPrice(map);//收益
		map.put("status", "0");
		BigDecimal dPrice = orderComDao.getComPrice(map);//添加的订单收益
		sPrice=sPrice.add(dPrice);
		memberCouponDao.updateCouponByTime(map);
		int couponCount = memberCouponDao.count(map);//可用优惠券数
		//提现总金额
		map.put("type","0");
		BigDecimal xPrice =  memberPriceDao.getSPriceByUser(map);   //消费  -
		map.put("type","1");
		BigDecimal tPrice =  memberPriceDao.getSPriceByUser(map);   //提现  -
		map.put("type","2");
		BigDecimal cPrice =  memberPriceDao.getSPriceByUser(map);   //充值  +
		map.put("type","3");
		BigDecimal fPrice =  memberPriceDao.getSPriceByUser(map);   //返利  +

		//BigDecimal canExPrice = fPrice.subtract(tPrice);   //余额


		BigDecimal allPrice =cPrice.add(tPrice).subtract(xPrice);
		BigDecimal canExPrice = new BigDecimal(0);
        if (xPrice.compareTo(cPrice) == 1) {  //消费大于充值
			canExPrice = fPrice.subtract(tPrice).subtract(xPrice.subtract(cPrice));
		} else {
			canExPrice = fPrice.subtract(tPrice);
		}
		Map<String,Object> priceMap = new HashMap<>();
		priceMap.put("extractMoney",tPrice);
		priceMap.put("allPrice",allPrice);
		priceMap.put("sPrice",sPrice);
		priceMap.put("couponCount",couponCount);
		priceMap.put("canExPrice",canExPrice);
		BigDecimal a = orderComDao.getMemberFreezeMoney(map.get("userId").toString());
		priceMap.put("freezeMoney",a);
		return priceMap;
	}

	@Override
	public R getMemberComList(Map<String, Object> paramMap) {
		if(!paramMap.get("type").toString().equals("4")){
			paramMap.put("delFlag","0");
			PageHelperUtils.page(paramMap);
			List<MemberPriceDO> list = memberPriceDao.list(paramMap);
			PageInfo<MemberPriceDO> pageInfo = new PageInfo<>(list);
			return R.ok().put("data", pageInfo);
		}else{
			//冻结金额
			PageHelperUtils.page(paramMap);
			List<OrderComDO> list = orderComDao.getMemberFreezeMoneyList(paramMap);
			if(list.size()>0){
				for (OrderComDO orderComDO : list) {
					OrderOrderDO orderOrderDO = orderOrderDao.get(orderComDO.getOrderId());
					if(orderOrderDO!=null){
						MemberUserDO memberUserDO = memberUserDao.get( orderOrderDO.getUserId());
						if(memberUserDO!=null){
							orderComDO.setUserId(memberUserDO.getName());
						}else{
							orderComDO.setUserId("");
						}
						
					}
					
				}
			}
			PageInfo<OrderComDO> pageInfo = new PageInfo<>(list);
			return R.ok().put("data", pageInfo);
		}
		
	}

    @Override
    public List<Map<String, Object>> getByExtractId(Map<String, Object> params) {
		List<Map<String,Object>> list = orderComDao.getByExtractId(params);
		return list;
    }

	@Override
	public int getByExtractIdCount(Map<String, Object> params) {
		int count = orderComDao.getByExtractIdCount(params);
		return count;
	}

	/**
	 * 可提现/待结算
	 * @param params
	 * @return
	 */
	@Override
	public Map<String, Object> getOrderComList(Map<String, Object> params) {
		Map<String,Object> returnMap = new HashMap<>();
		params.put("delFlag","0");
		if (params.get("type").toString().equals("0")){
			params.put("status",2);//可提现
			PageHelperUtils.page(params);
			List<Map<String, Object>> list = orderComDao.extractList(params);
			PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
			returnMap.put("pageInfo",pageInfo);
		}else if (params.get("type").toString().equals("1")){
			params.put("status","0");//待结算
			PageHelperUtils.page(params);
			List<Map<String, Object>> list = orderComDao.extractList(params);
			PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
			Map<String, Object> priceMap = this.getOrderCom(params);
			BigDecimal dPrice=(BigDecimal)priceMap.get("dPrice");
			returnMap.put("pageInfo",pageInfo);
			returnMap.put("dPrice",dPrice);
		}
		return returnMap;
	}

    @Override
    public int removeCom(String orderId) {
		return orderComDao.removeCom(orderId);

	}



}
